﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Text;

namespace OleDbNamedParameters
{
    public static partial class CommandXT
    {

        /// <summary>
        /// Replaces named parameters with question marks.
        /// Executes an SQL statement against System.Data.OleDb.OleDbConnection and returns the number of rows affected.
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// <exception cref="System.InvalidOperationException"></exception>
        public static int ExecuteNonQueryNamed(this OleDbCommand command)
        {
            command.UseNamedParameters();
            int ret = command.ExecuteNonQuery();
            command.ReturnNames();
            return ret;
        }

        /// <summary>
        /// Replaces named parameters with question marks.
        /// Sends the System.Data.OleDb.OleDbCommand.CommandText to the System.Data.OleDb.OleDbConnection and builds and System.Data.OleDb.OleDbDataReader.
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// <exception cref="System.InvalidOperationException"></exception>
        public static OleDbDataReader ExecuteReaderNamed(this OleDbCommand command)
        {
            return command.ExecuteReaderNamed(CommandBehavior.Default);
        }
        /// <summary>
        /// Replaces named parameters with question marks.
        /// Sends the System.Data.OleDb.OleDbCommand.CommandText to the System.Data.OleDb.OleDbConnection and builds and System.Data.OleDb.OleDbDataReader 
        /// using one of the System.Data.CommandBehavior values.
        /// </summary>
        /// <param name="command"></param>
        /// <param name="behavior"></param>
        /// <returns></returns>
        /// <exception cref="System.InvalidOperationException"></exception>
        public static OleDbDataReader ExecuteReaderNamed(this OleDbCommand command, CommandBehavior behavior)
        {
            command.UseNamedParameters();
            OleDbDataReader reader = command.ExecuteReader(behavior);
            command.ReturnNames();
            return reader;
        }

        /// <summary>
        /// Replaces named parameters with question marks.
        /// Executes the query, and returns the first column of the first row in the result set returned by the query. Additional rows or columns are ignored.
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// <exception cref="System.InvalidOperationException"></exception>
        public static object ExecuteScalarNamed(this OleDbCommand command)
        {
            command.UseNamedParameters();
            object ret = command.ExecuteScalar();
            command.ReturnNames();
            return ret;
        }
    }
}
